const {
  SvipModel
} = require('../../../utils/svip-model.js');
var svipModel = new SvipModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    layerModel: false,
    isPurchaseHistory: false, //购买记录
    isInstructions: false, //使用说明
    currentIndex: 1, //卡索引
    cardList: [], //门店卡列表
    shopId: "", //门店id
    couponList: [], //优惠券列表
    buyRecordList: [], //购买记录
    commodityList: [], //商品列表
    shopCardInfo: {}, //当前卡片详情
    isCouponMoreShow: false, //是否显示更多券
    isCommodityMoreShow: false, //是否显示更多商品
    isCouponData: false,
    isCommodityData: false,
    nowToggle: false, //是否显示游戏点
    gameCouponList: [], //游戏券列表
    isHints: false //是否有游戏券
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      shopId: options.shopId || ""
    }, function() {
      that.getSvipCardList(); //会员SVIP门店列表
    })
  },

  /**
   * 会员SVIP门店列表
   */
  getSvipCardList: function() {
    var that = this;
    svipModel.svipCardList({
      merchantId: wx.getStorageSync('merchantId'),
      shopId: that.data.shopId
    }).then(data => {
      var cardListData = [];
      for (var i = 0; i < data.length; i++) {
        var list = {};
        list.nickname = data[i].nickname;
        list.avatarUrl = data[i].avatarUrl;
        list.memberName = data[i].memberName;
        list.memberLevel = data[i].memberLevel || 0;
        list.memberLevelName = data[i].memberLevelName;
        list.cardCollar = data[i].cardCollar; //是否领取尊享黑卡 1:是 0:否
        list.sVipType = data[i].sVipType; //SVIP类型 1:是 0:否
        list.validBegin = data[i].validBegin.replace(/-/g, "."); //有效期开始时间
        list.validEnd = data[i].validEnd.replace(/-/g, ".").substr(0, 10); //有效期结束时间
        list.balanceDays = data[i].balanceDays; //剩余天数
        // list.pkgRemark = (data[i].pkgRemark || '').split('\n'); //使用说明
        // list.pkgDesc =(data[i].pkgDesc || '').split('\n'); //套餐描述
        list.pkgRemark = data[i].pkgRemark || ''; //使用说明
        list.pkgDesc =data[i].pkgDesc || ''; //套餐描述
        list.shopName = data[i].shopName;
        list.address = data[i].address;
        list.shopId = data[i].shopId;
        cardListData.push(list);
      }
      that.setData({
        cardList: cardListData,
        shopId: data[0].shopId,
        shopCardInfo: cardListData[0]
      }, function() {
        that.getSvipDiscountCoupon(); //获取svip优惠券列表
        that.getSvipBuyRecord(); //购买记录
        that.getSvipProductList(); //svip专享商品
        that.getSvipGameCoupon(); //svip专享游戏券
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 获取svip优惠券列表
   */
  getSvipDiscountCoupon() {
    var that = this;
    svipModel.svipDiscountCoupon({
      merchantId: wx.getStorageSync('merchantId'),
      shopId: that.data.shopId
    }).then(data => {
      if (data && data.length > 0) {
        var couponData = [];
        for (var i = 0; i < data.length; i++) {
          var list = {};
          list.couponName = data[i].couponName ? data[i].couponName : "-";
          list.validBegin = data[i].validBegin ? data[i].validBegin.replace(/-/g, "/").substr(0, 10) : "";
          list.validEnd = data[i].validEnd ? data[i].validEnd.replace(/-/g, "/").substr(0, 10) : "";
          list.giftCoinNum = data[i].promotionAmount ? data[i].promotionAmount : "0" || "0"; //赠币数
          list.discountNum = data[i].promotionAmount ? data[i].promotionAmount.toFixed(1) : "0.0" || "0.0"; //折扣数
          list.promotionAmount = data[i].promotionAmount ? data[i].promotionAmount.toFixed(2) : "0.00" || "0.00"; //满减金额
          var promotionAmount = list.promotionAmount.split(".");
          list.integer = promotionAmount[0];
          list.decimals = promotionAmount[1];
          list.couponType = data[i].couponType;
          list.accumAmount = data[i].accumAmount ? data[i].accumAmount : 0; //满额数量
          list.maxAmount = data[i].maxAmount ? data[i].maxAmount : 0; //最高消费数额
          list.discountLimit = data[i].discountLimit ? data[i].discountLimit : 0; //最高折扣金额
          list.assetStatus = data[i].assetStatus; //资产状态 0:未使用 1:已使用 2:已过期
          list.channels = data[i].channels; //渠道
          couponData.push(list);
        }
        that.setData({
          couponList: couponData,
          isCouponData: false
        })
      } else {
        that.setData({
          couponList: [],
          isCouponData: true
        })
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 获取svip商品
   */
  getSvipProductList() {
    var that = this;
    svipModel.svipProductList({
      merchantId: wx.getStorageSync('merchantId'),
      shopId: that.data.shopId,
      limit: 9999,
      offset: 0
    }).then(data => {
      if (data && data.length > 0) {
        that.setData({
          commodityList: data,
          isCommodityData: false
        })
      } else {
        that.setData({
          isCommodityData: true
        })
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 购买记录
   */
  getSvipBuyRecord() {
    var that = this;
    svipModel.svipBuyRecord({
      merchantId: wx.getStorageSync('merchantId'),
      shopId: that.data.shopId
    }).then(data => {
      if (data && data.length > 0) {
        var buyRecordData = [];
        for (var i = 0; i < data.length; i++) {
          var orderStatusName = "";
          if (data[i].orderStatus == 1 && data[i].payStatus == 0 && data[i].shippedStatus == 0) {
            orderStatusName = "待付款";
          } else if (data[i].orderStatus == 1 && data[i].payStatus == 1 && data[i].shippedStatus == 0) {
            orderStatusName = "待发货";
          } else if (data[i].orderStatus == 1 && data[i].payStatus == 1 && data[i].shippedStatus == 1) {
            orderStatusName = "待收货";
          } else if (data[i].orderStatus == 2) {
            orderStatusName = "已完成";
          } else if (data[i].orderStatus == 3) {
            orderStatusName = "已取消";
          } else {
            orderStatusName = "";
          }
          var list = {};
          list.amountPayable = data[i].amountPayable;
          list.balanceDays = data[i].balanceDays;
          list.orderAt = data[i].orderAt.replace(/-/g, ".").substr(0, 10);
          list.orderStatus = data[i].orderStatus;
          list.payStatus = data[i].payStatus;
          list.shippedStatus = data[i].shippedStatus;
          list.orderStatusName = orderStatusName;
          buyRecordData.push(list);
        }
        that.setData({
          buyRecordList: buyRecordData,
        })
      } else {
        that.setData({
          buyRecordList: [],
        })
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 获取门店id
   */
  changeIndicatorDots: function(event) {
    var that = this;
    var currentShopId = event.detail.currentItemId;
    var selectShop = that.data.cardList.filter(item => item.shopId == currentShopId);
    that.setData({
      shopId: currentShopId, //门店id
      shopCardInfo: selectShop[0],
      isCouponData: false,
      isCommodityData: false,
      couponList: [], //优惠券列表
      buyRecordList: [], //购买记录
      commodityList: [], //商品列表
    }, function() {
      that.getSvipDiscountCoupon(); //获取svip优惠券列表
      that.getSvipBuyRecord(); //购买记录
      that.getSvipProductList(); //svip专享商品
      that.getSvipGameCoupon(); //svip专享游戏券
    })
  },

  /**
   * 获取svip商品
   */
  getSvipGameCoupon() {
    var that = this;
    svipModel.svipGameCoupon({
      merchantId: wx.getStorageSync('merchantId'),
      shopId: that.data.shopId
    }).then(data => {
      if (data && data.length > 0) {
        var dataList = [];
        for (var i = 0; i < data.length; i++) {
          var list = {};
          list.assetType = data[i].assetType; //资产类型 3次卡 6券包
          list.validBegin = data[i].validBegin ? data[i].validBegin.replace(/-/g, "/").substr(0, 10) : ""; //开始时间
          list.validEnd = data[i].validEnd ? data[i].validEnd.replace(/-/g, "/").substr(0, 10) : ""; //失效时间
          list.amountAvail = data[i].amountAvail; //可用数量
          list.assetStatus = data[i].assetStatus; //资产状态，0:正常,1:冻结,2:失效, 3:锁定
          list.couponCode = data[i].couponCode; //游戏券编码
          list.dayLimit = (data[i].dayLimit == 0 || data[i].dayLimit == "0") ? "无限制" : "每天使用局数限制" + data[i].dayLimit + "局"; //每天限制局数
          list.total = data[i].total; //总局数
          list.gameCouponName = data[i].gameCouponName; //游戏券名称
          list.imgUrl = data[i].imgUrl; //游戏券图片
          list.machineDetail = data[i].machineDetail; //机台配置
          list.toggle = false;
          var machineDetailInfo = '';
          for (var j = 0; j < data[i].machineDetail.length; j++) {
            var item = data[i].machineDetail[j];
            if (data[i].assetType == 6) { //6券包局数，3次卡显示
              machineDetailInfo += item.machineName + "/" + (item.remain || 0) + "局" + (j < data[i].machineDetail.length - 1 ? "、" : "");
            } else if (data[i].assetType == 3) {
              machineDetailInfo += item.machineName + (j < data[i].machineDetail.length - 1 ? "、" : "");
            }
          }
          list.machineDetailInfo = machineDetailInfo;
          dataList.push(list);
        }
        that.setData({
          isHints: false,
          gameCouponList: dataList
        });
      } else {
        that.setData({
          gameCouponList: [],
          isHints: true
        })
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 关闭弹出层
   */
  closeLayer: function() {
    this.setData({
      layerModel: false
    })
  },
  /**
   * 购买记录
   */
  getRecord: function() {
    this.setData({
      layerModel: true,
      isPurchaseHistory: true,
      isInstructions: false
    })
  },

  /**
   * 使用说明
   */
  getInstructions: function() {
    this.setData({
      layerModel: true,
      isPurchaseHistory: false,
      isInstructions: true
    })
  },
  /**
   * 游戏点列表
   * */
  getContent: function(e) {
    this.setData({
      nowToggle: !this.data.nowToggle
    })
  },

  /**
   * 显示更多商品
   */
  commodityMore: function() {
    var that = this;
    that.setData({
      isCommodityMoreShow: !that.data.isCommodityMoreShow
    })
  },

  /**
   * 查看更多优惠券
   */
  goCouponMore: function() {
    wx.navigateTo({
      url: '/pages/memberCenter/discountCoupon/index'
    })
  },

  /**
   * 查看更多游戏券
   */
  goTicketMore: function() {
    wx.navigateTo({
      url: '/pages/memberCenter/gameTicket/index?shopId=' + this.data.shopId
    })
  },

  /**
   * 查看更多svip商品
   */
  goCurrencyShop(e) {
    wx.navigateTo({
      url: `/pages/currencyShop/chooseCurrency/index?shopName=${this.data.shopCardInfo.shopName}&address=${this.data.shopCardInfo.address}&shopId=${this.data.shopId}`
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  }
})
